package com.yuyou.fn.controller;

import com.yuyou.fn.analyze.service.ICostCenterService;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.Period;
import com.yuyou.fn.educational.service.IPeriodService;
import com.yuyou.fn.educational.vo.AttendClassLectureQueryVo;
import com.yuyou.fn.educational.vo.PeriodRelativeVo;
import com.yuyou.fn.educational.vo.TeacherQueryVo;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.entity.Oauth;
import com.yuyou.fn.platform.service.IAreaTeamService;
import com.yuyou.fn.platform.service.IOauthService;
import com.yuyou.fn.platform.service.IOpenAuthService;
import com.yuyou.fn.report.vo.*;
import com.yuyou.fn.wage.service.IWageTeacherDollarsService;
import com.yuyou.fn.wage.vo.TeacherDollarsDetailVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * Created by itachi on 2017/04/05.
 */
@Controller
@RequestMapping("/tas")
public class TasApiController extends ApiBaseController {





    @Resource
    private IOpenAuthService openAuthService;



    @Resource
    private IOauthService oauthService ;

    @ApiOperation(value = "获取教师openId" )
    @RequestMapping(value = "/findOpenIdByTeacherId" ,method = { RequestMethod.GET ,RequestMethod.POST } )
    @ResponseBody
    public ReturnResult<List<String>> findOpenIdByTeacherId( @RequestParam("teacherId") long teacherId,HttpServletRequest request  ) {
        checkSign(request);
        List<Oauth> oauthList = oauthService.findByUserIdRole(teacherId, 2);
        List<String> openIds = BeanUtils.toList(oauthList, "openId");
        return ReturnResult.success(openIds);
    }

    private void checkSign(HttpServletRequest request){
        String appId=request.getHeader("appId");
        String sign=request.getHeader("sign");
        String times=request.getHeader("times");
        Date date=new Date(Long.parseLong(times));
        Date preDate= TimeUtils.changeMinute(new Date(),-5);
        if (date.before(preDate)){
            throw new BusException("签名过期");
        }
        if (StringUtils.isEmpty(appId) || StringUtils.isEmpty(sign) || StringUtils.isEmpty(times)){
            throw new BusException("缺少参数");
        }
        String secretKey = openAuthService.findSecretKeyForAppId(appId);
        if(!sign.toLowerCase().equals(DigestUtils.md5Hex(appId  +secretKey+ times ).toLowerCase())){
            throw new BusException("检验失败");
        }
    }





}
